package com.es.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.es.dto.CategoryDTO;
import com.es.entity.goods.TbCategory;
import com.es.mapper.TbCategoryMapper;
import com.es.service.TbCategoryService;
import com.es.utils.BeanHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品类目表，类目和商品(spu)是一对多关系，类目与品牌是多对多关系 服务实现类
 * </p>
 *
 * @author HM
 * @since 2019-11-29
 */
@Service
public class TbCategoryServiceImpl extends ServiceImpl<TbCategoryMapper, TbCategory> implements TbCategoryService {

    /**
     * 根据ids 查询分类的集合
     * @param ids
     * @return
     */
    @Override
    public List<CategoryDTO> findCategoryListByIds(List<Long> ids) {
        Collection<TbCategory> tbCategories = this.listByIds(ids);
        List<CategoryDTO> categoryDTOList = tbCategories.stream()
                .map(tbCategory -> BeanHelper.copyProperties(tbCategory, CategoryDTO.class))
                .collect(Collectors.toList());
        if(CollectionUtils.isEmpty(categoryDTOList)){
            throw new IndexOutOfBoundsException();
        }
        return categoryDTOList;
    }
}
